home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3.iso / chapte13 / cmcmail.c < prev    next >
C/C++ Source or Header  |  1996-03-07  |  12KB  |  413 lines

  1.  
  2. #include <windows.h>  
  3. #include <commctrl.h>
  4. #include "cmc.h"
  5. #include "CMCMail.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "CMC Mail"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. HWND           hList = NULL;
  108. CMC_session_id lhSession = 0;
  109.  
  110. LPCTSTR pszColumns[] = { "Sender", "Subject", "Date" };
  111.  
  112.  
  113. VOID RefreshContents()
  114. {
  115.    CMC_message_summary*   result = NULL;
  116.    CMC_message_reference* reference = NULL;
  117.    int        nPos   = 0;
  118.    CMC_uint32 uCount = 1; 
  119.  
  120.    // Clear the list view contents.
  121.    //..............................
  122.    ListView_DeleteAllItems( hList );
  123.    
  124.    // Continue until there are no more items.
  125.    //........................................
  126.    while ( !CMCList( lhSession, NULL, CMC_ERROR_UI_ALLOWED, 
  127.                      ( reference ? reference : NULL ), &uCount,
  128.                      0, &result, NULL ) )
  129.    {
  130.       LV_ITEM    item;
  131.       TCHAR      szDate[32];
  132.  
  133.       // If there are no more items, break.
  134.       //...................................
  135.       if ( uCount == 0 )
  136.          break;
  137.  
  138.       reference = (CMC_message_reference*)HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 
  139.                                                      sizeof( CMC_message_reference )+
  140.                                                      result->message_reference->length );
  141.  
  142.       memcpy( reference, result->message_reference, sizeof( CMC_message_reference )+
  143.                                                     result->message_reference->length );
  144.  
  145.       item.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
  146.       item.iItem = nPos;
  147.       item.iImage = 0;
  148.       item.iSubItem = 0;
  149.       item.pszText = result->originator->name;
  150.       item.lParam  = (LPARAM)reference;
  151.       ListView_InsertItem( hList, &item );
  152.  
  153.       ListView_SetItemText( hList, nPos, 1, result->subject );
  154.  
  155.       wsprintf( szDate, "%2d/%2d/%4d", result->time_sent.month+1,
  156.                                        result->time_sent.day,
  157.                                        result->time_sent.year+1900 );
  158.       ListView_SetItemText( hList, nPos, 2, szDate );
  159.  
  160.       CMCFree( result );
  161.       result = NULL;
  162.    }
  163. }
  164.  
  165.  
  166. VOID DeleteMessage()
  167. {
  168.    LV_ITEM  item;
  169.    int      nIndex = -1;
  170.    CMC_message_reference* reference;
  171.  
  172.    while( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
  173.    {
  174.       item.mask = LVIF_PARAM;
  175.       item.iItem = nIndex;
  176.       item.iSubItem = 0;
  177.       ListView_GetItem( hList, &item );
  178.  
  179.       reference = (CMC_message_reference*)item.lParam;
  180.  
  181.       // Delete the item from the mailbox.
  182.       //..................................
  183.       if ( !CMCActOn( lhSession, reference, CMC_ACT_ON_DELETE, CMC_ERROR_UI_ALLOWED, 0, NULL ) )
  184.          HeapFree( GetProcessHeap(), 0, reference );
  185.    }
  186.  
  187.    RefreshContents();
  188. }
  189.  
  190.  
  191. VOID ReadMessage()
  192. {
  193.    LV_ITEM  item;
  194.    int      nIndex = -1;
  195.    CMC_message_reference* reference;
  196.  
  197.    // Get the selected list view item.
  198.    //.................................
  199.    if( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
  200.    {
  201.       CMC_message* message = NULL;
  202.  
  203.       item.mask = LVIF_PARAM;
  204.       item.iItem = nIndex;
  205.       item.iSubItem = 0;
  206.       ListView_GetItem( hList, &item );
  207.  
  208.       reference = (CMC_message_reference*)item.lParam;
  209.  
  210.       // Retrieve the message.
  211.       //......................
  212.       if ( !CMCRead( lhSession, reference, CMC_ERROR_UI_ALLOWED, &message, 0, NULL ) )
  213.       {
  214.          // Display the message contents.
  215.          //..............................
  216.          MessageBox( hList, message->text_note, message->subject, MB_OK );
  217.          CMCFree( message );
  218.       }
  219.    }
  220. }
  221.  
  222.  
  223. VOID SendMailMessage()
  224. {
  225.    CMC_message message;
  226.  
  227.    memset( &message, 0, sizeof( message ) );
  228.  
  229.    if ( !CMCSend( lhSession, &message, CMC_SEND_UI_REQUESTED, 0, NULL ) )
  230.       MessageBox( hList, "Message was sent.", "Send Message", MB_OK | MB_ICONINFORMATION );
  231.    else
  232.       MessageBox( hList, "Message could not be sent.", "Send Message", MB_OK | MB_ICONINFORMATION );
  233.  
  234. }
  235.  
  236.  
  237. VOID ShowProperties()
  238. {
  239.    LV_ITEM  item;
  240.    int      nIndex = -1;
  241.    CMC_message_reference* reference = NULL;
  242.    CMC_message_summary*   result = NULL;
  243.  
  244.    // Get the selected list view item.
  245.    //.................................
  246.    if( (nIndex = ListView_GetNextItem( hList, nIndex, LVNI_SELECTED )) > -1 )
  247.    {
  248.       CMC_message* message = NULL;
  249.       CMC_uint32   uCount = 1;
  250.  
  251.       if ( nIndex > 0 )
  252.       {
  253.          item.mask = LVIF_PARAM;
  254.          item.iItem = nIndex;
  255.          item.iSubItem = 0;
  256.          ListView_GetItem( hList, &item );
  257.  
  258.          reference = (CMC_message_reference*)item.lParam;
  259.       }
  260.  
  261.       // Retrieve the message.
  262.       //......................
  263.       if ( !CMCList( lhSession, NULL, CMC_ERROR_UI_ALLOWED, 
  264.                      ( reference ? reference : NULL ), &uCount,
  265.                      0, &result, NULL ) )
  266.       {
  267.          CMC_recipient* recipient = NULL;
  268.          // Show properties of sender.
  269.          //...........................
  270.          CMCLookup( lhSession, result->originator, CMC_LOOKUP_DETAILS_UI,
  271.                     0, &uCount, &recipient, NULL );
  272.          CMCFree( recipient );
  273.       }
  274.    }
  275. }
  276.  
  277.  
  278. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  279. {
  280.    switch( uMsg )
  281.    {
  282.       case WM_CREATE :
  283.               InitCommonControls();
  284.               if ( !CMCInitialize() )
  285.               {
  286.                  MessageBox( hWnd, "CMC is not supported!", NULL, MB_OK | MB_ICONSTOP );
  287.                  return( -1 );
  288.               }
  289.  
  290.               hList = CreateWindowEx( WS_EX_CLIENTEDGE, WC_LISTVIEW, "",
  291.                                       WS_CHILD | WS_VISIBLE | LVS_REPORT,
  292.                                       0, 0, 10, 10, hWnd, (HMENU)101, hInst, NULL );
  293.  
  294.               if ( hList )
  295.               {
  296.                  CMC_boolean bUIAvail;
  297.                  HIMAGELIST  hImage;
  298.                  LV_COLUMN   lvc;
  299.                  int         index;
  300.  
  301.                  lvc.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  302.                  lvc.fmt  = LVCFMT_LEFT;
  303.                  lvc.cx   = 150;
  304.  
  305.                  for (index = 0; index <= 2; index++)
  306.                  {
  307.                     lvc.iSubItem = index;
  308.                     lvc.pszText  = (LPTSTR)pszColumns[index];
  309.  
  310.                     ListView_InsertColumn( hList, index, &lvc );
  311.                  }
  312.  
  313.                  hImage = ImageList_Create( 16, 16, ILC_COLOR | ILC_MASK, 1, 1 );
  314.                  ImageList_AddIcon( hImage, LoadIcon( hInst, "RECMAIL" ) );
  315.                  ListView_SetImageList( hList, hImage, LVSIL_SMALL );
  316.  
  317.                  // Connect to mail service.
  318.                  //.........................
  319.                  if ( (CMCQueryConfiguration( 0, CMC_CONFIG_UI_AVAIL, 
  320.                                               &bUIAvail, NULL) == CMC_SUCCESS) && bUIAvail ) 
  321.                  {
  322.                     if ( CMCLogon( NULL, NULL, NULL, NULL, (CMC_ui_id) NULL, 
  323.                               CMC_VERSION, CMC_LOGON_UI_ALLOWED | CMC_ERROR_UI_ALLOWED, 
  324.                               &lhSession, NULL ) )
  325.                     {
  326.                        MessageBox( hWnd, "Could not log on to mail service!", NULL,
  327.                                    MB_OK | MB_ICONSTOP );
  328.  
  329.                        CMCUninitialize();
  330.                        return( -1 );
  331.                     }
  332.                  }
  333.  
  334.                  RefreshContents();
  335.               }
  336.               break;
  337.  
  338.       case WM_COMMAND :
  339.               switch( LOWORD( wParam ) )
  340.               {
  341.                  case IDM_REFRESH :
  342.                         RefreshContents();
  343.                         break;
  344.  
  345.                  case IDM_DELETE :
  346.                         DeleteMessage();
  347.                         break;
  348.  
  349.                  case IDM_OPEN :
  350.                         ReadMessage();
  351.                         break;
  352.  
  353.                  case IDM_NEW :
  354.                         SendMailMessage();
  355.                         break;
  356.  
  357.                  case IDM_PROPERTIES :
  358.                         ShowProperties();
  359.                         break;
  360.  
  361.                  case IDM_ABOUT :
  362.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  363.                         break;
  364.  
  365.                  case IDM_EXIT :
  366.                         DestroyWindow( hWnd );
  367.                         break;
  368.               }
  369.               break;
  370.       
  371.       case WM_SIZE :
  372.               MoveWindow( hList, 0, 0, LOWORD( lParam ), HIWORD( lParam ), TRUE );
  373.               break;
  374.  
  375.       case WM_DESTROY :
  376.               if ( lhSession > 0 )
  377.                  CMCLogoff( lhSession, (CMC_ui_id) NULL, 0, 0 );
  378.                  
  379.               CMCUninitialize();    
  380.               PostQuitMessage(0);
  381.               break;
  382.  
  383.       default :
  384.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  385.    }
  386.  
  387.    return( 0L );
  388. }
  389.  
  390.  
  391. LRESULT CALLBACK About( HWND hDlg,           
  392.                         UINT message,        
  393.                         WPARAM wParam,       
  394.                         LPARAM lParam)
  395. {
  396.    switch (message) 
  397.    {
  398.        case WM_INITDIALOG: 
  399.                return (TRUE);
  400.  
  401.        case WM_COMMAND:                              
  402.                if (   LOWORD(wParam) == IDOK         
  403.                    || LOWORD(wParam) == IDCANCEL)    
  404.                {
  405.                        EndDialog(hDlg, TRUE);        
  406.                        return (TRUE);
  407.                }
  408.                break;
  409.    }
  410.  
  411.    return (FALSE); 
  412. }
  413.